<template>
  <div class="login">
    <div class="header">
      <div class="title"><span>万卷书城</span></div>
      <div class="btns">
        <div class="list">
          <a href="javascript:;">链接按钮</a><a href="javascript:;">链接按钮</a>
        </div>
      </div>
    </div>
    <div class="container">
      <div class="left">
        <div v-if="show" class="content">
          <div class="login_header">
            <div class="login_title">登录</div>
            <div class="avatar">
              <div class="hidden" @click="show = !show">
                <span class="avatar_login">扫码登录</span>
                <img
                    src="https://p6.music.126.net/obj/wo3DlcOGw6DClTvDisK1/9656441793/9f07/c197/3af2/f07b8d6ef20964be159ce812841fc9d2.png"
                    alt=""
                />
              </div>
            </div>
          </div>
          <div class="user">
            <div class="phone"><a href="javascript:;">手机验证码登录</a></div>
            <div class="form">
              <div>
                <input v-model="user.username" type="text" placeholder="请输入用户名"/>
              </div>
              <div>
                <input
                    v-model="user.password"
                    type="password"
                    placeholder="请输入密码"
                />
              </div>
            </div>
            <div class="checkbox">
              <div>
                <label for="check"
                ><input v-model="isChecked" id="check" type="checkbox"/><span
                >下次自动登录</span
                ></label
                >
              </div>
              <span>忘记密码</span>
            </div>
            <button @click="login">登录</button>
          </div>
          <div class="sLogin">
            <ul>
              <li>
                <img
                    src="https://img2.baidu.com/it/u=3617372896,3784218432&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=483"
                    alt=""
                    class="icons"
                /><span>微信登录</span>
              </li>
              <li>
                <img
                    src="https://img2.baidu.com/it/u=4215241341,4086989804&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"
                    alt=""
                    class="icons"
                />
                <span>QQ登录</span>
              </li>
              <li>
                <img
                    src="https://img0.baidu.com/it/u=2411971627,942452162&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"
                    alt=""
                    class="icons"
                />
                <span>微博登录</span>
              </li>
            </ul>
          </div>
        </div>
        <div v-else class="show">
          <span>扫码登录</span>
          <img
              src="https://img2.baidu.com/it/u=1814268193,3619863984&fm=253&fmt=auto&app=138&f=JPEG?w=632&h=500"
              alt=""
          />
          <span class="back" @click="show = !show">选择其他登录模式</span>
        </div>
      </div>
      <div class="right">
        <div class="msg">
          <div>
            还没有账号?
            <router-link to="/register">立即注册</router-link>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import router from "@/router";
import {mapMutations, mapState} from "vuex";

export default {
  computed: {
    ...mapState(['url', "userId", "routerBeforeLoginTo", "isLogin"])
  },
  data() {
    return {
      show: true,
      user: {
        username: '',
        password: ''
      },
      isChecked: false,
    };
  },
  methods: {
    //合并本地购物车到数据库
    mergeLocalCart() {
      let localCart = JSON.parse(localStorage.getItem("cartTemp"))
      if (localCart==null){
        return
      }
      console.log("向服务器发送 localCart 数组")
      console.log(localCart)
      //需要数据：书名、价格、图片、数量
      axios.post("/cart/getCartList", localCart).then(r => {
        if (r.data.state === 20000) {
          console.log("服务器发来购物车数据：")
          let carts = r.data.data
          for (let i = 0; i < localCart.length; i++) {
            carts[i].bookId = localCart[i].bookId
          }
          console.log(carts)
          console.log("处理后其作为参数发给服务器合并购物车")
          axios.post("/cart/mergeLocalCart/" + this.userId,carts).then(r => {
            if (r.data.state === 20000) {
              console.log("成功合并本地购物车数据到服务器，清理本地购物车数据")
              localStorage.removeItem("cartTemp")
            } else {
              this.$message.error("合并本地购物车数据到服务器 失败")
            }
          })
        }
      }).catch(e => console.log(e))
    },
    //从 vueX 中获得设置 loginJWT 的方法
    ...mapMutations(["setUserId", "setIsLogin", "setUser"]),
    login() {
      axios.post(this.url + "/user/login", this.user).then(r => {
        if (r.data.state === 20000) {
          this.$message.success("登录成功")
          /*登录成功后把 jwt 保存*/
          console.log("登录数据")
          console.log(r.data.data)
          localStorage.setItem("Authentication", r.data.data.longinJwtStr)
          localStorage.setItem("uid", r.data.data.id)
          this.setUserId(r.data.data.id)
          this.setIsLogin(true)
          console.log(this.isLogin)
          //获取头像、昵称
          console.log("获取头像昵称请求中 userId ： " + this.userId)
          axios.get("/user/menuSource/" + this.userId).then(r => {
            let data = {nick: '', phone: '', email: '', avatar: ''}
            data.nick = r.data.data.nick
            data.avatar = r.data.data.avatar
            this.setUser(data)
            //登录成功后合并本地购物车数据到服务器
            this.mergeLocalCart()
            localStorage.removeItem("cartTemp")
            console.log(this.user.avatar)
          }).catch(e => console.log(e))
          /*根据之前要去的 url 判断，如果原本就是登录直接去首页*/
          if (this.routerBeforeLoginTo === '/login') {
            router.push('/')
          } else {
            router.push(this.routerBeforeLoginTo)
          }
        } else {
          this.$message.error(r.data.message)
        }
      })
    },
  },
};
</script>

<style scoped>
.msg {
  margin-left: 40px;
  font-size: 0.9rem;
  background-color: rgb(233, 233, 233);
  width: 100%;
  height: 40px;
  line-height: 40px;
  padding-left: 10px;
  border-radius: 5px;
}

.msg a {
  color: rgb(0, 119, 255);
}

.show {
  width: 300px;
  margin: 0 auto;
  text-align: center;
}

.show span {
  display: block;
  margin: 10px 0;
}

.show img {
  width: 100%;
  height: 100%;
}

.show :first-child {
  font-weight: bold;
  font-size: 1.2rem;
}

.show .back {
  border: 1px solid rgb(214, 214, 214);
  background-color: rgb(247, 247, 247);
  height: 30px;
  display: block;
  line-height: 30px;
  border-radius: 30px;
  cursor: pointer;
}

.show .back:hover {
  background-color: rgb(236, 236, 236);
}

ul {
  list-style: none;
}

a {
  text-decoration: none;
  color: gray;
}

input {
  outline: none;
  border: none;
  display: block;
}

.login {
  margin: 0 10px;
  font-family: "微软雅黑 light";
}

.header {
  border-top: 1px solid rgb(228, 228, 228);
  border-bottom: 1px solid rgb(228, 228, 228);
  height: 70px;
  display: flex;
  justify-content: space-around;
  align-items: center;
  position: relative;
  width: 100%;
  background-color: rgb(247, 247, 247);
}

.title {
  color: rgb(129, 30, 30);
  font-size: 2.5rem;
  font-family: "方正舒体";
  font-weight: bold;
}

.btns {
  height: 100%;
  width: 140px;
}

.list {
  position: absolute;
  bottom: 0px;
  display: flex;
}

.btns a {
  display: block;
  text-decoration: none;
  color: gray;
  margin: 0 5px;
  font-size: 0.8rem;
  position: relative;
  transition: all 0.3s;
}

.btns a:hover {
  color: rgb(0, 174, 255);
  transform: scale(1.1);
  font-weight: bold;
  transition: all 0.3s;
}

.container {
  margin-top: 40px;
  display: flex;
}

.container > .left {
  width: 50%;
  border-right: 1px solid rgb(175, 175, 175);
  box-sizing: border-box;
}

.container > .left > .content {
  margin: 0 auto;
  width: 300px;
}

.container > .left > .content > .login_header {
  display: flex;
  justify-content: space-between;
  height: 80px;
}

.container > .left > .content > .login_header > .avatar .hidden img {
  width: 50px;
  height: 50px;
  transform: rotate(-90deg);
  margin-top: 18px;
}

.container > .left > .content > .login_header > .avatar {
  position: relative;
  cursor: pointer;
}

.container > .left > .content > .login_header > .login_title {
  font-size: 1.5rem;
  font-weight: bold;
  margin: auto 0;
}

.container > .left > .content > .login_header > .avatar .avatar_login {
  position: absolute;
  top: 0;
  width: 100%;
  font-size: 0.7rem;
}

.user > .phone {
  text-align: right;
  font-size: 0.8rem;
  margin-bottom: 3px;
  margin-top: 20px;
}

.form {
  border: 1px solid gray;
  border-radius: 5px;
  overflow: hidden;
}

.form div {
  height: 35px;
}

.form div input {
  box-sizing: border-box;
  display: block;
  height: 100%;
  width: 100%;
  padding: 2px 10px;
}

.form div:first-child input {
  border-bottom: 1px solid rgb(163, 163, 163);
}

.checkbox {
  font-size: 0.8rem;
  line-height: 15px;
  display: flex;
  margin-top: 10px;
  margin-bottom: 30px;
  justify-content: space-between;
}

.checkbox div {
  display: flex;
  cursor: pointer;
  user-select: none;
}

.checkbox div input {
  width: 15px;
  height: 15px;
  margin-right: 3px;
}

label {
  display: flex;
}

.user button {
  width: 100%;
  height: 35px;
  background-color: rgb(32, 151, 248);
  color: white;
  border: none;
  border-radius: 4px;
  letter-spacing: 4px;
  margin-bottom: 30px;
}

.user button:active {
  font-size: 0.8rem;
  transform: translateY(1px);
  background-color: rgb(32, 198, 248);
}

.sLogin ul {
  border-top: 1px solid gainsboro;
  display: flex;
  justify-content: space-evenly;
  padding-top: 10px;
}

.sLogin li {
  display: flex;
  align-items: center;
  padding: 5px;
  border-radius: 5px;
}

.sLogin li:hover {
  cursor: pointer;
  background-color: rgb(206, 206, 206);
  color: white;
}

.sLogin li span {
  font-size: 0.8rem;
}

.icons {
  display: block;
  width: 20px;
  height: 20px;
  border-radius: 20px;
}

.right {
  width: 50%;
}
</style>
